(function(){

    jQuery(function($) {
        $(window).autoHeight();
        var dateArray = new Array();
        var now       = new Date();
        for (var i = 0; i < 2; i++) {
            var recDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() + i);
            var year  = recDate.getFullYear();
            var month = recDate.getMonth() + 1;
            var date  = recDate.getDate();
            if (month < 10) { month = '0' + month; }
            if (date  < 10) { date  = '0' + date;  }
            dateArray.push(year + '-' + month + '-' + date);
        }

        $.getData('/people/@owner/@self', {})
        .next(function(owner){
            var ownerInfo = owner[0];
            getSchedule(ownerInfo.id, dateArray);
            $('.owner_name').text(ownerInfo.nickname);

            $('#send_feed').click(function(){
                if (!$('#feed_message').val()) {
                    alert('お誘いを入力してください。');
                    return;
                }
            });
        })
        .error(function(e){
            //alert(''+e);
        });

        $('#link_to_canvas').click(function(){
            var view = gadgets.views.getSupportedViews()["canvas"];
            gadgets.views.requestNavigateTo(view);
        });
    });

    function getSchedule (ownerId, dateArray) {
        if (!dateArray) return;
        var apiUrl = apiUrlPrefix + 'schedule_select_list.php?member_id=' + ownerId +
            '&start_date=' + dateArray[0] + '&end_date=' + dateArray[1] + '&check=1&stamp=' + (new Date).getTime();
        //alert(apiUrl);
        var params = {};
        if (isRelease)
            params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
        params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
        gadgets.io.makeRequest(apiUrl, function(ret){
            if (!ret || !ret.data || ret.data.return_code != 0) {
                //alert('スケジュールの取得に失敗しました');
                return;
            }
            if (ret.data.result) {
                $.each(ret.data.result, function(i, schedule) {
                    var index = $.inArray(schedule.start_date, dateArray);
                    $('.hima_status').eq(index).text('暇だなぁ');
                });
            }
        }, params);
        return;
    }

})();
